package com.gzz.utils;

import com.alibaba.fastjson.JSONObject;
import com.gzz.common.DeviceInfo;
import eu.bitwalker.useragentutils.UserAgent;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
// 工具类
public class IPUtil {
	private static final String KEY = "XRGBZ-VYFW5-XW3IF-IC6O7-WDZDT-MBBIP";
	public static String getIpAddr(HttpServletRequest request) {

		String ip = request.getHeader("x-forwarded-for");

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Real-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("http_client_ip");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip != null && ip.indexOf(",") != -1) {
			ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();
		}
		return ip;
	}
	public static DeviceInfo getDevice(HttpServletRequest request){
		UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("user-agent"));
		String clientType = userAgent.getOperatingSystem().getDeviceType().toString();

		String os = userAgent.getOperatingSystem().getName();

		String ip = IPUtil.getIpAddr(request);

		String browser = userAgent.getBrowser().toString();

		return new DeviceInfo(clientType,os,ip,browser);

	}

	//使用腾讯的接口通过ip拿到城市信息

	public static String getCityInfo(String ip)  {
		String s = sendGet(ip, KEY);
		Map map = JSONObject.parseObject(s, Map.class);
		String message = (String) map.get("message");
		if("query ok".equals(message)){
			Map result = (Map) map.get("result");
			Map addressInfo = (Map) result.get("ad_info");
			String nation = (String) addressInfo.get("nation");
			String province = (String) addressInfo.get("province");
			//  String district = (String) addressInfo.get("district");
			String city = (String) addressInfo.get("city");
			String address = nation + "-" + province + "-" + city;
			return address;
		}else{
			System.out.println(message);
			return null;
		}
	}
	//根据在腾讯位置服务上申请的key进行请求操作
	public static String sendGet(String ip, String key) {
		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = "https://apis.map.qq.com/ws/location/v1/ip?ip="+ip+"&key="+key;
			URL realUrl = new URL(urlNameString);
			// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			// 设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 建立实际的连接
			connection.connect();
			// 获取所有响应头字段
			Map<String, List<String>> map = connection.getHeaderFields();
			// 遍历所有的响应头字段
//            for (Map.Entry entry : map.entrySet()) {
//                System.out.println(key + "--->" + entry);
//            }
			// 定义 BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常！" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输入流
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return result;
	}


}
